#include "stdafx.h"
#include "wf_drawcircle3p.h"

#include "pf_snapper.h"



WF_DrawCircle3P::WF_DrawCircle3P(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw circles",
                           container, graphicView) {
    reset();
}



WF_DrawCircle3P::~WF_DrawCircle3P() {}


QAction* WF_DrawCircle3P::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// "Circle: 3 Points"
    QAction* action = new QAction(tr("3 Points"), NULL);
	action->setIcon(QIcon(":/extui/circles3p.png"));
    //action->zetStatusTip(tr("Draw circles with 3 points"));
    return action;
}



void WF_DrawCircle3P::reset() {
    data.reset();
    point1 = PF_Vector(false);
    point2 = PF_Vector(false);
    point3 = PF_Vector(false);
}



void WF_DrawCircle3P::init(int status) {
    PF_PreviewActionInterface::init(status);

    reset();
}



void WF_DrawCircle3P::trigger() {
    PF_PreviewActionInterface::trigger();

    preparePreview();
    if (data.isValid()) {
        PF_Circle* circle = new PF_Circle(container,
                                          data);
        circle->setLayerToActive();
        circle->setPenToActive();
        container->addEntity(circle);

        // upd. undo list:
        if (document!=NULL) {
            document->startUndoCycle();
            document->addUndoable(circle);
            document->endUndoCycle();
        }
        PF_Vector rz = graphicView->getRelativeZero();
		graphicView->redraw(LX_Define::RedrawDrawing);
        graphicView->moveRelativeZero(rz);
        drawSnapper();

        setStatus(SetPoint1);
        reset();
    } else {
        PF_DIALOGFACTORY->requestWarningDialog(tr("Invalid circle data."));
    }
}


void WF_DrawCircle3P::preparePreview() {
    data.reset();
    if (point1.valid && point2.valid && point3.valid) {
        PF_Circle circle(NULL, data);
        bool suc = circle.createFrom3P(point1, point2, point3);
        if (suc) {
            data = circle.getData();
        }
    }
}



void WF_DrawCircle3P::mouseMoveEvent(PF_MouseEvent* e) {
    PF_Vector mouse = snapPoint(e);

    switch (getStatus()) {
    case SetPoint1:
        point1 = mouse;
        break;

    case SetPoint2:
        point2 = mouse;
        break;

    case SetPoint3:
        point3 = mouse;
        preparePreview();
        if (data.isValid()) {
            PF_Circle* circle = new PF_Circle(preview, data);

            deletePreview();
            preview->addEntity(circle);
            drawPreview();
        }
        break;
    }
}



void WF_DrawCircle3P::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawCircle3P::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }
    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetPoint1:
        point1 = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetPoint2);
        break;

    case SetPoint2:
        point2 = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetPoint3);
        break;

    case SetPoint3:
        point3 = mouse;
        trigger();
        break;

    default:
        break;
    }
}



void WF_DrawCircle3P::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }
}



PF_StringList WF_DrawCircle3P::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}


void WF_DrawCircle3P::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetPoint1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"),
                                            tr("Cancel"));
        break;
    case SetPoint2:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify second point"),
                                            tr("Back"));
        break;
    case SetPoint3:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify third point"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DrawCircle3P::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawCircle3P::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarCircles);
    }
}


// EOF

